package tool

import java.io.File

import play.api.mvc.RequestHeader
import implicits.Implicits._
import models.Tables._
import shared.Pojo._
import tool.plot._

/**
 * Created by yz on 13/7/2020
 */
trait ProjectFileTool {

  def getUserIdDir(userId: Int) = {
    new File(Tool.userDir, userId.toString)
  }

  def getStandardFile(dir: File) = {
    new File(dir, "standard.txt")
  }

  def getAllMetRawFile(dir: File) = {
    new File(dir, "AllMet_Raw.txt")
  }

  def getOtuBiomFile(dir: File) = {
    new File(dir, "otu_table.biom")
  }

  def getOtuTaxaBiomFile(dir: File) = {
    new File(dir, "otu_taxa_table.biom")
  }

  def getOtuFile(dir: File) = {
    new File(dir, "otu_table.txt")
  }

  def getRawGroupFile(dir: File) = {
    new File(dir, "raw_group.txt")
  }

  def getAvatarDir(userId: Int) = {
    val userIdDir = Tool.getUserIdDir(userId)
    new File(userIdDir, "avatar")
  }

  def getAvatarFile(userId: Int) = {
    val avatarDir = Tool.getAvatarDir(userId)
    new File(avatarDir, "avatar.png")
  }

  def getDefaultAvatarFile = {
    val avatarDir = Tool.dataAvatarDir
    new File(avatarDir, "default.jpg")
  }

  def getUserMissionDir(implicit request: RequestHeader) = {
    val userIdDir = Tool.getUserIdDir
    new File(userIdDir, "mission")
  }

  def getUserMissionDir(userId: Int) = {
    val userIdDir = Tool.getUserIdDir(userId)
    new File(userIdDir, "mission")
  }

  def getUserDataDir(userId: Int) = {
    val userIdDir = Tool.getUserIdDir(userId)
    new File(userIdDir, "file_data")
  }

  def getUserMissionKindDir(kind: String)(implicit request: RequestHeader) = {
    val userMissionDir = Tool.getUserMissionDir
    new File(userMissionDir, kind)
  }

  def getUserMissionKindDir(userId: Int, kind: String) = {
    val userMissionDir = Tool.getUserMissionDir(userId)
    new File(userMissionDir, kind)
  }

  def getUserDataKindDir(userId: Int, kind: String) = {
    val userMissionDir = Tool.getUserDataDir(userId)
    new File(userMissionDir, kind)
  }

  def getUserDataGroupDir(userId: Int) = {
    val userMissionDir = Tool.getUserDataDir(userId)
    new File(userMissionDir, GroupDataKind.name)
  }

  def getUserDataMatrixDir(userId: Int) = {
    val dir = Tool.getUserDataDir(userId)
    new File(dir, MetDataKind.name)
  }

  def getUserDataKindDir(row: FileDataRow) = {
    val userDataDir = Tool.getUserDataDir(row.userId)
    new File(userDataDir, row.kind)
  }

  def getUserDataKindFile(row: FileDataRow) = {
    val kindDir = Tool.getUserDataKindDir(row)
    new File(kindDir, row.fileName)
  }

  def getUserDataGroupFile(userId: Int, fileName: String) = {
    val kindDir = Tool.getUserDataGroupDir(userId)
    new File(kindDir, fileName)
  }

  def getUserDataMatrixFile(userId: Int, fileName: String) = {
    val kindDir = Tool.getUserDataMatrixDir(userId)
    new File(kindDir, fileName)
  }

  def getExampleFile(names: List[String]) = {
    val dir = new File(Tool.softExampleDir, names(1))
    new File(dir, names(2))
  }

  def getGroupFile(userId: Int, names: List[String]): File = {
    if (names(0) == "example") {
      Tool.getExampleFile(names)
    } else Tool.getUserDataGroupFile(userId, names(1))
  }

  def getGroupFile(userId: Int, fileName: String): File = {
    val names = fileName.mySplit("/")
    getGroupFile(userId, names)
  }

  def getMatrixFile(userId: Int, names: List[String]): File = {
    if (names(0) == "example") {
      Tool.getExampleFile(names)
    } else Tool.getUserDataMatrixFile(userId, names(1))
  }

  def getMatrixFile(userId: Int, fileName: String): File = {
    val names = fileName.mySplit("/")
    getMatrixFile(userId, names)
  }

  def getUserIdDir(implicit request: RequestHeader) = {
    val userId = Tool.getUserId
    new File(Tool.userDir, userId.toString)
  }

  def getLogFile(dir: File) = {
    val file = new File(dir, "log.txt")
    "Run successfully!".toFile(file)
    file
  }

  def getInputFile(dir: File) = {
    new File(dir, "input.csv")
  }

  def getMatrixFile(dir: File) = {
    new File(dir, "matrix.txt")
  }

  def getColorJsonFile(dir: File) = {
    new File(dir, "color.json")
  }

  def getDiffMetaboliteFile(dir: File) = {
    new File(dir, "Diff_Metabolite.csv")
  }

  def getMissionIdDir(mission: MissionRow) = {
    val userMissionKindDir = getUserMissionKindDir(mission.userId, mission.kind)
    new File(userMissionKindDir, mission.id.toString)
  }

  def getMissionWorkspaceDir(mission: MissionRow) = {
    val missionIdDir = getMissionIdDir(mission)
    new File(missionIdDir, "workspace")
  }

  def getMissionResultDir(mission: MissionRow) = {
    val missionIdDir = getMissionIdDir(mission)
    new File(missionIdDir, "result")
  }

  def getConfigFile(workspaceDir: File) = {
    new File(workspaceDir, "config.json")
  }

  def getSampleColorFile(workspaceDir: File) = {
    new File(workspaceDir, "Class_Color.txt")
  }

  def getSampleColorFile(workspaceDir: File, name: String) = {
    new File(workspaceDir, Tool.getSampleColorFileName(name))
  }

  def getSampleColorFileName(name: String) = {
    s"${name}_Class_Color.txt"
  }

  def getPlotConfigTxtFile(workspaceDir: File) = {
    new File(workspaceDir, "plot_config.txt")
  }

  def getPlotConfigTxtFile(workspaceDir: File, kind: String): File = {
    val curRPlot = Tool.kindRPlotMap(kind)
    new File(workspaceDir, curRPlot.configFileName)
  }

  def getCalculateConfigTxtFile(workspaceDir: File) = {
    new File(workspaceDir, "calculate_config.txt")
  }

  def getCalculateConfigJsonFile(workspaceDir: File) = {
    new File(workspaceDir, "calculate_config.json")
  }

  def getGroupFile(workspaceDir: File) = {
    new File(workspaceDir, "group.txt")
  }

}
